from selenium.webdriver.common.by import By
from .base import BasePage, BaseHandle
from hmAutoTest.utils import UtilsDriver


class LoginPage(BasePage):
    def __init__(self):
        super().__init__()
        self.username = (By.NAME, "username")
        self.password = (By.NAME, "password")
        self.login_btn = (By.ID, "inp1")

    def find_username(self):
        return self.get_element(self.username)

    def find_password(self):
        return self.get_element(self.password)

    def find_login_btn(self):
        return self.get_element(self.login_btn)


class LoginHandle(BaseHandle):
    def __init__(self):
        self.login_page = LoginPage()

    def input_username(self, username):
        self.input_text(self.login_page.find_username(), username)

    def input_password(self, password):
        self.input_text(self.login_page.find_password(), password)

    def click_login_btn(self):
        js = "document.getElementById('inp1').removeAttribute('disabled')"
        self.login_page.driver.execute_script(js)
        self.login_page.find_login_btn().click()


class LoginProxy:
    def __init__(self):
        self.login_handle = LoginHandle()

    def login(self, username, password):
        self.login_handle.input_username(username)
        self.login_handle.input_password(password)
        self.login_handle.click_login_btn()